package com.huluobo.a2203kotlindouyindemo.home

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2203kotlindouyindemo.R
import com.huluobo.a2203kotlindouyindemo.adapter.HomeAdapter
import com.huluobo.a2203kotlindouyindemo.adapter.SearchAdapter
import com.huluobo.a2203kotlindouyindemo.databinding.FragmentHomeBinding
import com.huluobo.base.bean.SearchBean
import com.huluobo.base.bean.VideoBean
import com.huluobo.base.view.BaseMVVMFragment

class HomeFragment : BaseMVVMFragment<FragmentHomeBinding, HomeViewModel>() {
    private lateinit var homeAdapter: HomeAdapter
    private val videoList = mutableListOf<VideoBean>()

    private lateinit var searchAdapter: SearchAdapter
    private val searchList = mutableListOf<SearchBean>()
    override fun getLayoutId(): Int = R.layout.fragment_home

    override fun initData() {
        viewModel.getVideos(1, 20)

        viewModel.getVideosSuccess.observe(this) {
            if (it.code == 200) {
                videoList.clear()
                videoList.addAll(it.data)
                homeAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.getVideosFailed.observe(this) {
            Log.i("TagA", "获取首页视频类表失败:$it")
        }

        viewModel.findVideosSuccess.observe(this) {
            if (it.code == 200) {
                videoList.clear()
                videoList.addAll(it.data)
                homeAdapter.notifyDataSetChanged()
            } else {
                ToastUtils.showLong(it.message)
            }
        }

        viewModel.findVideosFailed.observe(this) {
            Log.i("TagA", "搜索视频失败$it")
        }

        viewModel.querySearchSuccess.observe(this) {
            searchList.clear()
            searchList.addAll(it)
            searchAdapter.notifyDataSetChanged()
        }

        viewModel.querySearchFailed.observe(this) {
            Log.i("TagA", "查询搜索记录失败:$it")
        }
    }

    override fun initView() {
        dataBinding.homeRv.layoutManager = GridLayoutManager(requireContext(), 2)
        homeAdapter = HomeAdapter(videoList)
        dataBinding.homeRv.adapter = homeAdapter

        dataBinding.homeSearchRv.layoutManager = GridLayoutManager(requireContext(), 5)
        searchAdapter = SearchAdapter(searchList)
        dataBinding.homeSearchRv.adapter = searchAdapter

        dataBinding.homeSearchBtn.setOnClickListener {
            val keyword = dataBinding.homeSearchEt.text.toString()
            if (keyword.isNotEmpty()) {
                viewModel.findVideos(keyword)
                viewModel.saveSearch(keyword)
            } else {
                viewModel.getVideos(1, 20)
            }
        }

        dataBinding.homeQueryBtn.setOnClickListener {
            viewModel.querySearch()
        }
    }

    override fun getViewModel(): Class<HomeViewModel> {
        return HomeViewModel::class.java
    }
}